package adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.projectmoudle_two.R;

import java.util.List;

/**
 * Created by eyesee on 2017/6/4.
 */

public class ListAdapter extends BaseAdapter {

    private final List<String>  mList;
    private final Context       mContext;

    public ListAdapter (List<String> list , Context context) {
        this.mList = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mList==null?0:mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.list_item,null);
            holder.mTextView = (TextView) convertView.findViewById(R.id.tv);
            convertView.setTag(holder);
        }else {
            holder=((ViewHolder) convertView.getTag());

            setData(position,holder);
        }
        return convertView;
    }

    private void setData(int position, ViewHolder holder) {
        holder.mTextView.setText(mList.get(position));
    }

    class ViewHolder{
        TextView mTextView;
        ImageView mImageView;
    }
}
